<template>
  <el-dialog title="选择设备" v-model="visibleMachine" width="60%" append-to-body>
    <el-row :gutter="20">
      <!--点位数据-->
      <el-col :span="6" :xs="24">
        <div class="position-box">
          <el-radio-group v-model="positionTypeId" @change="getPositionType(positionTypeId)">
            <div class="title2">· 普通点位</div>
            <el-radio
              v-for="(item, index) in positionTypeOptions"
              :key="index"
              :label="item.positionTypeId"
              :value="item.positionTypeId"
              >{{ item.typeName }}</el-radio
            >
            <div class="title3">· 特殊点位</div>
            <el-radio
              v-for="(item, index) in specialPositionTypeOptions"
              :key="index"
              :label="item.positionTypeId"
              :value="item.positionTypeId"
              >{{ item.typeName }}</el-radio
            >
          </el-radio-group>
        </div>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
          <el-form-item label="设备类型" prop="machineType">
            <el-select
              v-model="queryParams.machineType"
              @change="getList('search')"
              placeholder="请选择"
              filterable
              style="width: 120px"
            >
              <el-option
                v-for="dict in device_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备编号" prop="machineId">
            <el-input
              class="form-input"
              clearable
              style="width: 200px"
              v-model="queryParams.machineId"
              placeholder="设备编号"
            />
          </el-form-item>
          <el-form-item label="设备名称" prop="machineName">
            <el-input
              class="form-input"
              v-model="queryParams.machineName"
              maxlength="12"
              style="width: 200px"
              clearable
              placeholder="设备名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSearchResect" size="small">重置</el-button>
            <el-button type="primary" @click="getList('search')" size="small">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="tableListAll"
          max-height="580"
          @selection-change="handleSelectionChange"
          ref="tableAllRef"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column
            label="设备编号"
            align="center"
            prop="machineId"
            :show-overflow-tooltip="true"
            min-width="90"
          />
          <el-table-column
            label="设备名称"
            align="center"
            prop="machineName"
            :show-overflow-tooltip="true"
            min-width="90"
          />
          <el-table-column
            label="设备标签"
            align="center"
            prop="machineTag"
            :show-overflow-tooltip="true"
            min-width="90"
          />
          <el-table-column
            label="投放地点"
            align="center"
            prop="positionAddr"
            :show-overflow-tooltip="true"
            min-width="90"
          />
        </el-table>
      </el-col>
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeSelectMachine">取 消</el-button>
        <el-button type="primary" @click="confirmSelected">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineProps, nextTick } from 'vue'
import { getMachineList } from '@/api/advert'
import { getPointTypeList } from '@/api/organization/pointType'
const props = defineProps({
  machindData: {
    type: Object,
    default: () => {}
  },
  resetDeviceSelection: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['selectedMachindData'])
const { proxy } = getCurrentInstance()
const { device_type } = proxy.useDict('device_type')

//** 设备标签 && 点位类型 && 设备类型  */
const positionTypeId = ref(null)
const positionTypeOptions = ref([]) // 普通点位
const specialPositionTypeOptions = ref([]) // 特殊点位
function getOptions() {
  console.log('queryParams.value.cityName', queryParams.value.cityName)
  getPointTypeList({
    pageNum: 1,
    pageSize: 1000,
    enable: 1,
    cityName: queryParams.value.cityName
  }).then((r) => {
    positionTypeOptions.value = r?.rows?.filter((i) => i?.type === 2)
    specialPositionTypeOptions.value = r?.rows?.filter((i) => i?.type === 1)
  })
}
//** 设备列表 */
const queryParams = ref({
  positionTypeId: null,
  machineType: '3',
  enable: 1,
  machineId: '',
  machineName: ''
})
const tableListAll = ref([])
const tableList = ref([])
const tableAllRef = ref(null)
function getList(search) {
  getMachineList(queryParams.value).then((res) => {
    tableListAll.value = res.data
    if (props.machindData || tableList.value) {
      tableList.value = props.machindData ? props.machindData.machineList : tableList.value
      let selectedIds = tableList.value.map((item) => item.machineId)
      tableListAll.value.forEach((row) => {
        if (selectedIds.includes(row.machineId)) {
          nextTick(() => {
            tableAllRef.value.toggleRowSelection(row, true)
          })
        } else {
          nextTick(() => {
            tableAllRef.value.toggleRowSelection(row, false)
          })
        }
      })
    }

    if (search === 'search') {
      let searchSelectedIds = selected.value.map((item) => item.machineId)
      setTimeout(() => {
        tableListAll.value.forEach((ele) => {
          if (searchSelectedIds.includes(ele.machineId)) {
            tableAllRef.value.toggleRowSelection(ele, true)
          }
        })
      }, 100)
    }
  })
}

function handleSearchResect() {
  positionTypeId.value = null
  queryParams.value.positionTypeId = null
  queryParams.value.machineType = '3'
  queryParams.value.machineName = ''
  queryParams.value.machineId = ''
  queryParams.value.enable = 1
  getList('search')
}
function getPositionType(positionTypeId) {
  queryParams.value.positionTypeId = positionTypeId
  getList('search')
}
//** 选择设备 */
const visibleMachine = ref(false)
function handleAddFileColumn(cityName) {
  queryParams.value.cityName = cityName
  getOptions()
  handleSearchResect()
  if (props.resetDeviceSelection) {
    tableList.value = null
  }
  visibleMachine.value = true
}
const selected = ref([])
const handleSelectionChange = (val) => {
  selected.value = val
}
const confirmSelected = () => {
  tableList.value = selected.value
  closeSelectMachine()
  emit('selectedMachindData', tableList.value, queryParams.value.machineType)
}
const closeSelectMachine = () => {
  visibleMachine.value = false
  selected.value = []
}

defineExpose({
  handleAddFileColumn
})
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 10px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.select-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .input-box {
    width: 80%;
  }
}
:deep(.el-radio) {
  width: 100%;
}
.position-box {
  background-color: #f2f2f2;
  width: 100%;
  min-height: 300px;
  max-height: 600px;
  padding: 8px;
  overflow-y: scroll;
  overflow-x: hidden;
  .title2 {
    width: 100%;
    margin-bottom: 10px;
    font-size: 14px;
    font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
      Arial, sans-serif;
  }
  .title3 {
    width: 100%;
    margin: 20px 0 5px 0;
    font-size: 14px;
    font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
      Arial, sans-serif;
  }
}
</style>
